<template>
  <div class="token-stats-management">
    <!-- 统计概览卡片 -->
    <el-row :gutter="20" class="overview-cards">
      <el-col :span="6">
        <el-card class="overview-card">
          <div class="card-content">
            <div class="card-icon total-tokens">
              <i class="el-icon-data-line" />
            </div>
            <div class="card-info">
              <div class="card-title">总Token数</div>
              <div class="card-value">{{ overviewData.totalTokens || 0 }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="overview-card">
          <div class="card-content">
            <div class="card-icon total-cost">
              <span class="yuan-icon">￥</span>
            </div>
            <div class="card-info">
              <div class="card-title">总成本</div>
              <div class="card-value">¥{{ overviewData.totalCost || 0 }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="overview-card">
          <div class="card-content">
            <div class="card-icon active-users">
              <i class="el-icon-user" />
            </div>
            <div class="card-info">
              <div class="card-title">活跃用户</div>
              <div class="card-value">{{ overviewData.activeUsers || 0 }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="overview-card">
          <div class="card-content">
            <div class="card-icon total-chats">
              <i class="el-icon-chat-line-square" />
            </div>
            <div class="card-info">
              <div class="card-title">总对话数</div>
              <div class="card-value">{{ overviewData.totalChats || 0 }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 日期范围选择器 -->
    <el-card class="filter-card">
      <div class="filter-content">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          @change="handleDateRangeChange"
        />
        <el-button type="primary" :loading="loading" @click="refreshData">
          <i class="el-icon-refresh" /> 刷新数据
        </el-button>
        <el-button type="success" :loading="exportLoading" @click="exportData">
          <i class="el-icon-download" /> 导出数据
        </el-button>
      </div>
    </el-card>

    <!-- 用户排行表格 -->
    <el-card class="table-card">
      <div slot="header" class="card-header">
        <span>用户Token使用排行</span>
        <el-button type="text" @click="refreshUserRanking">
          <i class="el-icon-refresh" /> 刷新
        </el-button>
      </div>

      <el-table
        v-loading="userRankingLoading"
        :data="userRankingData"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="rank" label="排名" width="80" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.rank <= 3" :type="getRankType(scope.row.rank)">
              {{ scope.row.rank }}
            </el-tag>
            <span v-else>{{ scope.row.rank }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="用户名" min-width="120" align="center" />
        <el-table-column prop="userUid" label="用户ID" min-width="150" align="center" />
        <el-table-column prop="totalTokens" label="Token使用量" width="120" align="center">
          <template slot-scope="scope">
            {{ formatNumber(scope.row.totalTokens) }}
          </template>
        </el-table-column>
        <el-table-column prop="totalCost" label="使用成本" width="100" align="center">
          <template slot-scope="scope">
            ¥{{ (scope.row.totalCost && scope.row.totalCost.toFixed(2)) || '0.00' }}
          </template>
        </el-table-column>
        <el-table-column prop="chatCount" label="对话次数" width="100" align="center" />
        <el-table-column prop="avgTokensPerChat" label="平均Token/对话" width="120" align="center">
          <template slot-scope="scope">
            {{ formatNumber(scope.row.avgTokensPerChat) }}
          </template>
        </el-table-column>
        <el-table-column prop="lastChatTime" label="最后活跃时间" width="160" align="center">
          <template slot-scope="scope">
            {{ formatDateTime(scope.row.lastChatTime) }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          :current-page="pagination.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 模型使用统计 -->
    <el-card class="model-stats-card">
      <div slot="header" class="card-header">
        <span>模型使用统计</span>
        <el-button type="text" @click="refreshModelStats">
          <i class="el-icon-refresh" /> 刷新
        </el-button>
      </div>

      <el-table
        v-loading="modelStatsLoading"
        :data="modelStatsData"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="modelName" label="模型名称" align="center" />
        <el-table-column prop="usageCount" label="使用次数" align="center" />
        <el-table-column prop="totalTokens" label="Token使用量" align="center">
          <template slot-scope="scope">
            {{ formatNumber(scope.row.totalTokens) }}
          </template>
        </el-table-column>
        <el-table-column prop="totalCost" label="使用成本" align="center">
          <template slot-scope="scope">
            ¥{{ (scope.row.totalCost && scope.row.totalCost.toFixed(2)) || '0.00' }}
          </template>
        </el-table-column>
        <el-table-column prop="avgResponseTime" label="平均响应时间(ms)" align="center">
          <template slot-scope="scope">
            {{ formatNumber(scope.row.avgResponseTime) }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import tokenStatsMixin from '@/mixins/tokenStatsMixin'

export default {
  name: 'TokenStatsManagement',
  mixins: [tokenStatsMixin],
  data() {
    return {
      loading: false,
      exportLoading: false,
      userRankingLoading: false,
      modelStatsLoading: false
    }
  },

  created() {
    this.initDateRange()
    this.loadOverviewData()
    this.loadUserRankingData()
    this.loadModelStatsData()
  }
}
</script>

<style lang="scss" scoped>
.token-stats-management {
  padding: 20px;

  .overview-cards {
    margin-bottom: 20px;

    .overview-card {
      .card-content {
        display: flex;
        align-items: center;
        padding: 10px 0;

        .card-icon {
          width: 60px;
          height: 60px;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 15px;

          i {
            font-size: 24px;
            color: white;
          }

          .yuan-icon {
            font-size: 24px;
            color: white;
            font-weight: bold;
            font-family: 'Arial', sans-serif;
          }

          &.total-tokens {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          }

          &.total-cost {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
          }

          &.active-users {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
          }

          &.total-chats {
            background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
          }
        }

        .card-info {
          flex: 1;

          .card-title {
            font-size: 14px;
            color: #666;
            margin-bottom: 8px;
          }

          .card-value {
            font-size: 24px;
            font-weight: bold;
            color: #333;
          }
        }
      }
    }
  }

  .filter-card {
    margin-bottom: 20px;

    .filter-content {
      display: flex;
      align-items: center;
      gap: 15px;
    }
  }

  .table-card, .model-stats-card {
    margin-bottom: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }

  .pagination-container {
    margin-top: 20px;
    text-align: right;
  }
}
</style>
